/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.EnumSet;
import java.util.Hashtable;

import org.lwjgl.util.Rectangle;

import fi.honkalampisaatio.lmodeller.LanguageResource;
import fi.honkalampisaatio.lmodeller.LModeller;
import fi.honkalampisaatio.lmodeller.Logger;
import fi.honkalampisaatio.lmodeller.UserSetup;
import fi.honkalampisaatio.lmodeller.sound.BackgroundMusic;
import fi.honkalampisaatio.lmodeller.ui.GameButton.TEXTLOCATION;
import fi.honkalampisaatio.lmodeller.xml.User;
import glapp.GLApp;

public class SetupMenu extends GameView implements ActionListener {
	public SetupMenu() {
		super(EnumSet.of(BUTTONFLAGS.BACK, BUTTONFLAGS.HOME, BUTTONFLAGS.UPLABEL, BUTTONFLAGS.VOICE));
	}

	String musicFolder;
	GameListMenu mp3fileMenu;
	GameButton toggleMusicButton;
	//GameTextLabel titleLabel;
	Hashtable<String, UserSetup> userSettings;
	User activeUser;
	
	@Override
	public void init() {
		userSettings = new Hashtable<String, UserSetup>();
		activeUser = null;
    	int h = GLApp.getHeight()/10;
    	int w = GLApp.getWidth()/10;
    	int minFontSize, tmpFontSize;

    	tmpFontSize = setUpLabelText(LanguageResource.getInstance().get("CHOOSE_MUSIC"));
    	minFontSize = tmpFontSize;
    	toggleMusicButton = new GameButton(new Rectangle((int)(w*4.6),(int)(h*8.1),
    			(int)(w*0.8),(int)(h*0.8)),GameTextures.getInstance().getTexture("SOUND_BUTTON"), LModeller.actionColor,
    			null, TEXTLOCATION.UP);
		BackgroundMusic.getInstance().setOn(false);
		toggleMusicButton.setOnOff(false);

    	mp3fileMenu = new GameListMenu(new Rectangle((int)(w*1.0), (int)(h*1), 
    			(int)(w*8.0), (int)(h*7)), (int)(h*0.8));

    	mp3fileMenu.setMaxFontSize(minFontSize);
    	setMaxFontSize(minFontSize);

    	mp3fileMenu.setToolTipSentence(LanguageResource.getInstance().get("CHOOSE_MUSIC"));
    	toggleMusicButton.setToolTipSentence(LanguageResource.getInstance().get("MUSIC_ON"),
    			LanguageResource.getInstance().get("MUSIC_OFF"));
    	setUpToolTipSentence(LanguageResource.getInstance().get("CHOOSE_MUSIC"));
    	setInstructionSentence(LanguageResource.getInstance().get("CHOOSE_MUSIC"));

    	super.addComponent(mp3fileMenu);
    	mp3fileMenu.addActionListener(this);
    	mp3fileMenu.setActionCommand("MUSIC_MENU");
    	super.addComponent(toggleMusicButton);
    	toggleMusicButton.addActionListener(this);
    	toggleMusicButton.setActionCommand("MUSIC_BUTTON");
	}

	public void setMusicFolder(String musicFolder) {
		mp3fileMenu.clear();
		this.musicFolder = musicFolder.replace('\\', '/');
		if(!this.musicFolder.endsWith("/"))
		{
			this.musicFolder = this.musicFolder + "/";
		}
		File folder = new File(this.musicFolder);
		    
		if (folder.exists()) {
			File[] listOfFiles = folder.listFiles();
		    for (int i = 0; i < listOfFiles.length; i++) {
		    	if (listOfFiles[i].isFile()) {
		    		String filename = listOfFiles[i].getName();
			    	String ext = filename.substring(filename.lastIndexOf('.')+1,filename.length());
			    	if (ext.equalsIgnoreCase("mp3")) {  
			    		mp3fileMenu.addFile(filename);
			      	}
		    	}
		    }
		}
	}
	
	public void refreshUserSettings()
	{
		if(activeUser!=null && userSettings.containsKey(Integer.toString(activeUser.getUserId())))
		{
			UserSetup setup = userSettings.get(Integer.toString(activeUser.getUserId()));
			if(setup.isMusicOn())
			{
				toggleMusicButton.setOnOff(true);
				BackgroundMusic.getInstance().setOn(true);
			}
			else
			{
				toggleMusicButton.setOnOff(false);
				BackgroundMusic.getInstance().setOn(false);
			}
	    	mp3fileMenu.setSelectedIndex(setup.getSelectedMusicIndex());
			String itemText = mp3fileMenu.getSelectedText();
			if(itemText!=null && !itemText.isEmpty())
			{
				BackgroundMusic.getInstance().setMusic(this.musicFolder+itemText);
			}
		}
		else
		{
			toggleMusicButton.setOnOff(false);
			BackgroundMusic.getInstance().setOn(false);
	    	mp3fileMenu.setSelectedIndex(0);
			String itemText = mp3fileMenu.getSelectedText();
			if(itemText!=null && !itemText.isEmpty())
			{
				BackgroundMusic.getInstance().setMusic(this.musicFolder+itemText);
			}
		}
	}

	public void setUser(User user) {
		this.activeUser = user;
		if(!userSettings.containsKey(user.getUserId()))
		{
			UserSetup setup = new UserSetup();
			setup.setMusicOn(false);
			setup.setSelectedMusicIndex(0);
			userSettings.put(Integer.toString(user.getUserId()), setup);
		}
		refreshUserSettings();
	}

	@Override
	protected void refreshPage(int page) {
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("MUSIC_MENU"))
		{
			String itemText = mp3fileMenu.getSelectedText();
			if(itemText!=null && !itemText.isEmpty())
			{
				if(activeUser!=null && userSettings.containsKey(Integer.toString(activeUser.getUserId())))
				{
					UserSetup setup = userSettings.get(Integer.toString(activeUser.getUserId()));
					setup.setSelectedMusicIndex(mp3fileMenu.getSelectedIndex());
				}
				refreshUserSettings();
				Logger.getInstance().log("Setup Menu: Music "+itemText+" selected");
				BackgroundMusic.getInstance().setMusic(this.musicFolder+itemText);
			}
		}
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("MUSIC_BUTTON"))
		{
			if(activeUser!=null && userSettings.containsKey(Integer.toString(activeUser.getUserId())))
			{
				UserSetup setup = userSettings.get(Integer.toString(activeUser.getUserId()));
				setup.setMusicOn(!setup.isMusicOn());
			}
			refreshUserSettings();
		}
	}

}
